#region Using directives
using System;
using System.Data;
using WP.DB;
using WP.MF.DataEntity;
using WP.Commons;
#endregion

namespace WP.MF.DataAccess
{
    public class MLogRptDAL
    {
        public DBAccess m_dBAccess = null;
        public string m_strSql = string.Empty;
        public bool m_bIsPrintAll = false;

        public MLogRptDAL()
        { 
            
        }

        public bool SetRptData()
        {
            string strSql = string.Empty;
            string strOrderBy = string.Empty;

            DataFunctions.DelTemporyTable(m_dBAccess, "#TmpMLogRpt");

            strSql = @"CREATE TABLE #TmpMLogRpt (
				[RecId] [int] IDENTITY (1, 1) NOT NULL , 
	            [UserName] [char] (20) NOT NULL ,
	            [UDate] [datetime] NULL ,
	            [UTime] [char] (5) NOT NULL ,
	            [USys] [char] (3) NOT NULL ,
	            [DocNo] [char] (8) NOT NULL ,
            	[CustSupNo] [char] (8) NOT NULL ,
	            [Remark] [char] (200) NULL
				) ON [PRIMARY]";
            m_dBAccess.ExecuteSql(strSql);

            if (m_strSql.IndexOf("AS table2 ORDER BY") != -1)
            {
                strOrderBy = m_strSql.Substring(m_strSql.IndexOf("AS table2 ORDER BY"));
            }

            if (m_bIsPrintAll)
                m_strSql = "SELECT RecId, UserName, UDate, UTime, USys, DocNo, CustSupNo, Remark FROM MLog";
            else
                m_strSql = m_strSql.Remove(m_strSql.IndexOf("AS table2 ORDER BY") + 10);

            strOrderBy = strOrderBy.Replace("AS table2", "");
            strSql = "Insert into #TmpMLogRpt(UserName, UDate, UTime, USys, DocNo, CustSupNo, Remark) ";
            strSql += "select UserName, UDate, UTime, USys, DocNo, CustSupNo, Remark from (" + m_strSql + ") as a " + strOrderBy;
            m_dBAccess.ExecuteSql(strSql);

            //strSql = "select * into #TmpMLogRpt ";
            //strSql += "from (" + m_strSql + ") as a ";

            return true;
        }
    }
}

